<template>
  <div class="company">
    <img :src="store.topPic" class="top-pic" />

    <div class="company-menu">
      <div
        class="menu-item"
        @click="$router.push(item.path)"
        v-for="item in menus"
        :key="item.index"
        :class="{ active: $route.name == item.pathName }"
      >
        {{ item.name }}
      </div>
    </div>

    <router-view :store="store" :goodsList="goodsList"></router-view>

    <div class="desc-wrapper">
      <div class="desc-item">
        <div class="desc-label">业务联系电话</div>
        <div class="desc-value">{{ store.cphone }}</div>
      </div>
      <div class="desc-item">
        <div class="desc-label">联系人</div>
        <div class="desc-value">{{ store.cname }}</div>
      </div>
      <div class="desc-item">
        <div class="desc-label">地址</div>
        <div class="desc-value">{{ store.address }}</div>
      </div>

      <div class="desc-item">
        <div class="desc-label">网址</div>
        <div class="desc-value">
          <a :href="store.web">{{ store.web }}</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getStore, getGoods } from "@/api/account";
import storage from "@/utils/storage";
export default {
  data() {
    return {
      store: {
        users: {}
      },
      goodsList: [],
      id: this.$route.params.id
    };
  },
  computed: {
    menus() {
      if (this.id === "preview") {
        return [
          {
            pathName: "CompanyHome",
            index: "1",
            name: "商家首页"
          }
        ];
      } else if (this.id === "goods-preview") {
        return [
          {
            pathName: "GoodsDetail",
            index: "1",
            name: "商品详情"
          }
        ];
      }
      const menus = [
        {
          index: "1",
          name: "商家首页",
          pathName: "CompanyHome",
          path: `/company/${this.id}`
        },
        {
          index: "2",
          name: "所有商品",
          path: `/company/${this.id}/goods`,
          pathName: "GoodsList"
        }
      ];

      return menus;
    }
  },
  created() {
    if (this.id == "preview" || this.id === "goods-preview") {
      this.store = storage.getItem("STORE") || {};
    } else {
      this.getStore();
      this.getGoods();
    }
  },
  methods: {
    async getStore() {
      const res = await getStore({ userId: this.id, status: 3 });
      this.store = res.data;
    },
    async getGoods() {
      const res = await getGoods({ userId: this.id, status: 3 });
      this.goodsList = res.data.list;
    }
  }
};
</script>

<style lang="less" scoped>
.company {
  .top-pic {
    width: 100%;
    height: 240px;
    object-fit: cover;
  }

  .desc-wrapper {
    padding: 20px 40px;
    margin-top: 20px;
    display: flex;
    flex-wrap: wrap;
    background: @white;

    .desc-item {
      display: flex;
      width: 50%;
      font-size: 15px;
      padding: 10px;

      .desc-label {
        width: 120px;
        color: @text-color-2;
      }

      .desc-value {
        flex: 1;
      }
    }
  }

  .company-menu {
    display: flex;
    background: @red-7;
    padding-left: 10px;
    align-items: center;

    .menu-item {
      color: @white;
      padding: 12px 18px;
      font-size: 16px;
      cursor: pointer;

      &.active {
        font-weight: bold;
      }
    }
  }

  .company-wrapper {
    background: @white;
    .banner-wrapper {
      width: 600px;
      margin: auto;
    }

    /deep/.html-wrapper {
      width: 800px;
      margin: auto;
      padding: 20px 0 40px;

      p {
        margin-bottom: 20px;
      }
    }

    .goods-section {
      padding: 0 30px;
      .goods-list {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        .goods-item {
          border: 1px solid @border-color;
          width: 23%;
          margin-bottom: 16px;
          border-radius: 8px;
          overflow: hidden;

          .money {
            padding: 12px 12px 8px;
            span {
              color: @red;
              font-weight: bold;
              font-size: 16px;
            }
          }

          .title {
            padding: 0 12px 12px;
          }
        }
      }
    }
  }

  .goods-wrapper {
    padding: 24px 32px;
    background: @white;

    .goods-info {
      display: flex;
      .left-wrapper {
        .goods-pic {
          width: 400px;
        }
      }

      .right-wrapper {
        flex: 1;
        padding-left: 32px;

        .title-section {
          border-bottom: 1px dashed @border-color;
          padding-bottom: 16px;
          margin-bottom: 16px;

          .title {
            font-weight: bold;
            font-size: 20px;
          }

          .desc {
            margin: 12px 0;
            font-size: 16px;
          }
        }
      }
    }
  }

  .v-title {
    margin: 30px 0;
  }
}
</style>
